package com.chuji.crmcj.act;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.ListViewAdapter;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class FamilyConditionActivity extends Activity {
    //返回
    private ImageView mIv_Back;
    //显示
    private ListView lv_checkBox;
    //选择
    String[] beans = new String[] { "父母", "夫妻", "孩子", "在孕", "单身"};
    //适配器
    ListViewAdapter adapter;
    //请求码
    private int resultCode = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_condition);
        mIv_Back = (ImageView) findViewById(R.id.iv_back);
        //初始化数据
        lv_checkBox= (ListView) findViewById(R.id.lv_checkBox);
        //初始化适配器
        adapter=new ListViewAdapter(FamilyConditionActivity.this, beans);
        //填充适配器
        lv_checkBox.setAdapter(adapter);
        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        // this.finish();
    }
    //自定义适配器
    class ListViewAdapter extends BaseAdapter {
        private Context context;
        private String[] beans;
        //集合
        ArrayList<String> list= new ArrayList<String>();
        // 用来控制CheckBox的选中状况
        private HashMap<Integer, Boolean> isSelected;

        class ViewHolder {
            TextView tvName;
            CheckBox cb;
            LinearLayout ll_genjinfangshi;
        }

        public ListViewAdapter(Context context, String[] beans) {
            // TODO Auto-generated constructor stub
            this.beans = beans;
            this.context = context;
            isSelected = new HashMap<Integer, Boolean>();
            // 初始化数据
            initDate();
        }

        // 初始化isSelected的数据
        private void initDate() {
            for (int i = 0; i < beans.length; i++) {
                getIsSelected().put(i, false);
            }
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return beans.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return beans[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // 页面
            final ViewHolder holder;
            final String bean = beans[position];
            LayoutInflater inflater = LayoutInflater.from(context);
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.listview_family_condition, null);
                holder = new ViewHolder();
                holder.cb = (CheckBox) convertView.findViewById(R.id.cb_familycondition);
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_device_name);
                holder.ll_genjinfangshi = (LinearLayout) convertView.findViewById(R.id.ll_genjinfangshi);
                convertView.setTag(holder);
            } else {
                // 取出holder
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(bean);
            holder.ll_genjinfangshi.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    if (holder.cb.isChecked() == false) {
                        holder.cb.setChecked(true);
                    } else {
                        holder.cb.setChecked(false);
                    }
                    if (isSelected.get(position)) {
                        isSelected.put(position, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(position, true);
                        list.add(beans[position]);
                        UIUtils.runInMainThread(new Runnable() {
                            @Override
                            public void run() {
                                mIv_Back.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //传值
                                        Intent mIntent = new Intent();
                                        mIntent.putStringArrayListExtra("reason", list);
                                        // 设置结果，并进行传送
                                        setResult(resultCode, mIntent);
                                        finish();
                                    }
                                });
                            }
                        });
//                    UIUtils.showToastSafe("选中了" + position + "的" + beans[position]);
                        setIsSelected(isSelected);
                    }

                }
            });
            // 监听checkBox并根据原来的状态来设置新的状态
            holder.cb.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    if (isSelected.get(position)) {
                        isSelected.put(position, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(position, true);
                        list.add(beans[position]);
                        UIUtils.runInMainThread(new Runnable() {
                            @Override
                            public void run() {
                                mIv_Back.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //传值
                                        Intent mIntent = new Intent();
                                        mIntent.putStringArrayListExtra("family", list);
                                        // 设置结果，并进行传送
                                        setResult(resultCode, mIntent);
                                        finish();
                                    }
                                });
                            }
                        });
//                    UIUtils.showToastSafe("选中了" + position + "的" + beans[position]);
                        setIsSelected(isSelected);
                    }

                }
            });
            // 根据isSelected来设置checkbox的选中状况
            holder.cb.setChecked(getIsSelected().get(position));
            return convertView;
        }

        public HashMap<Integer, Boolean> getIsSelected() {
            return isSelected;
        }

        public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
            this.isSelected = isSelected;
        }
    }
    //屏蔽系统返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键

        }
        return false;
    }
}
